/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.sioc.aop;

import com.jspx.utils.ArrayUtil;
import com.jspx.boot.EnvFactory;
import com.jspx.sioc.BeanFactory;
import com.jspx.sioc.AopAppBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jspx.utils.StringUtil;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2006-11-7
 * Time: 11:36:10
 * com.jspx.sioc.aop.AopAppCommandImpl
 */
public class AopAppCommandImpl extends AbstractAopBean
{

    final private static Logger log = LoggerFactory.getLogger(AopAppCommandImpl.class);
    final private static BeanFactory beanFactory = EnvFactory.getBeanFactory();
    private String[] beanArray;
    private boolean enable = true;

    public AopAppCommandImpl()
    {

    }

    public boolean isEnable()
    {
        return enable;
    }

    public void setEnable(boolean enable)
    {
        this.enable = enable;
    }

    public void setBeanArray(String[] beanArray)
    {
        this.beanArray = beanArray;
    }

    public String[] getBeanArray()
    {
        return beanArray;
    }


    public String init()
    {
        if (!enable) return NONE;
        if (beanArray != null)
        {
            for (String beanKey : beanArray)
            {
                log.info("aop start " + beanKey);
                AopAppBean aopBean = null;
                if (beanKey.contains("@"))
                {
                    aopBean = (AopAppBean) beanFactory.getBean(StringUtil.substringBefore(beanKey,"@"), StringUtil.substringAfter(beanKey,"@"));
                } else
                    aopBean = (AopAppBean) beanFactory.getBean(beanKey, null);
                if (aopBean != null)
                {
                    try
                    {
                        aopBean.init();
                    } catch (Exception ex)
                    {
                        ex.printStackTrace();
                        return ERROR;
                    }
                }
            }
        }
        return SUCCESS;
    }

    public String destroy()
    {
        if (!enable) return NONE;
        if (beanArray != null)
        {
            for (String beanKey : beanArray)
            {
                AopAppBean aopBean = null;
                if (beanKey.contains("@"))
                {
                    aopBean = (AopAppBean) beanFactory.getBean(StringUtil.substringBefore(beanKey, "@"), StringUtil.substringAfter(beanKey, "@"));
                } else
                    aopBean = (AopAppBean) beanFactory.getBean(beanKey, null);
                if (aopBean != null)
                {
                    try
                    {
                        log.info(aopBean.getClass().getName() + "=" + aopBean.destroy());
                    } catch (Exception ex)
                    {
                        log.error(ex.getMessage() + " AOP Class:" + aopBean.getClass().getName());
                        return ERROR;
                    }
                }

            }
        }
        return SUCCESS;
    }

    public String toString()
    {
        return ArrayUtil.toString(beanArray, StringUtil.SEMICOLON);
    }
}